import json
import logging
from websocket_server import WebSocketServer

def load_config():
    try:
        with open('config.json', 'r') as f:
            return json.load(f)
    except Exception as e:
        logging.error(f"Error loading config: {e}")
        return None

def setup_logging(config):
    logging_config = config.get('logging', {})
    logging.basicConfig(
        level=logging_config.get('level', 'INFO'),
        format=logging_config.get('format', '%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
        filename=logging_config.get('file', 'websocket.log')
    )

def main():
    config = load_config()
    if not config:
        print("Error: Could not load configuration")
        return

    setup_logging(config)
    
    ws_config = config.get('websocket', {})
    server = WebSocketServer()
    
    try:
        logging.info(f"Starting WebSocket server on {ws_config.get('host')}:{ws_config.get('port')}")
        server.run(
            host=ws_config.get('host', '0.0.0.0'),
            port=ws_config.get('port', 8080)
        )
    except Exception as e:
        logging.error(f"Server error: {e}")

if __name__ == '__main__':
    main() 